package com.wang.userscenter.servlet.back;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson2.JSONObject;
import com.wang.userscenter.common.controller.BaseController;
import com.wang.userscenter.common.util.ConstatFinalUtil;
import com.wang.userscenter.users.pojo.Aadmins;
import com.wang.userscenter.users.service.UsersDbService;

/**
 * 这个是不需要登录的请求;
 * 
 * <pre>
	 * 需要登录的/back/模块名/方法名; http://127.0.0.1:8080/exec_proj_userscenter/back/AdminsBack/main
	 * 不登录的不要以/back开头;      http://127.0.0.1:8080/exec_proj_userscenter/NoLogin/adminsLogin
	 * 登录分为两步:
	 * 	打开登录页面,让用户输入用户名和密码
	 * 	点击登录,验证是成功还是失败
 * </pre>
 */
@Controller
@RequestMapping("/NoLogin")
public class NoLoginController  extends BaseController {
	@Autowired
	private UsersDbService usersDbService;
	
	/**
	 * 登录提交页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/adminsLoginSubmit")
	public String adminsLoginSubmit(HttpSession session, String email,String password, String code,
			Model model){
		log.info("==adminsLoginSubmit==");
		String info = "";
		/* 接收参数 */
		/* 获取到图片中的code */
		String sessionCode = session.getAttribute("rand") + "";
		log.info("==email==" + email);
		log.info("==password==" + password);
		log.info(code + "==password==" + password);
		if (email == null || "".equalsIgnoreCase(email)) {
			info = "请输入邮箱";
		} else {
			if (sessionCode.equalsIgnoreCase(code)
					||
				ConstatFinalUtil.SYS_CONFIG_JSON.getString(ConstatFinalUtil.INFO_ALL_RIGHT_CODE).equalsIgnoreCase(code)) {
				Map<String,Object> condMap = new HashMap<String, Object>();
				condMap.put("email", email);
				Aadmins admins = this.usersDbService.selectAdminsOne(condMap);
				/* 关联对象 */
				if (admins != null) {
					/* 根据邮箱查询到了===验证密码 */
					if (this.encryptUtil.match(admins.getPassword(), password)) {
						/* 密码正确==看状态 */
						if (admins.getStatus() == 1) {
							/* 登录成功 */
							/* 发票 */
							session.setAttribute("adminsSess", admins);
							session.setAttribute("lastLoginTime", admins.getLastLoginTime());
							/* 更新上次登录时间 */
							admins.setLastLoginTime(new Date());
							JSONObject resultDbJson = this.usersDbService.updateAdminsOne(admins);
							log.info("==管理员操作数据库结果:==" + resultDbJson);
							//response.sendRedirect(request.getContextPath() + "/back/AdminsBack/main");
							return "redirect:/back/AdminsBack/main";
						} else {
							info = "账号被禁用,请联系管理员";
						}
					} else {
						/* 密码不正确 */
						info = "密码不正确";
					}
					model.addAttribute("email", email);
				} else {
					/* 邮箱填写不正确 */
					info = "邮箱填写不正确";
				}
			} else {
				/* 验证码不正确 */
				info = "验证码不正确";
			}
		}
		model.addAttribute("info", info);
		return this.adminsLogin(session);
	}

	/**
	 * 打开登录页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/adminsLogin")
	public String adminsLogin(HttpSession session){
		log.info("==adminsLogin==");
		/* 获取对象,此对象是登录的时候放进去;
		 * 如果有,说明登录了.(买票)
		 * 如果没有,说明没有登录,(没有买票,去买票)
		 *  */
		Aadmins admins = (Aadmins) session.getAttribute("adminsSess");
		if (admins != null) {
			/* 说明用户肯定是登录了 */
			return "redirect:/back/AdminsBack/main";
		}
		return "/back/adminsLogin";
	}
}
